//require('../../../lib/lazyload/lazyload.js')
define(['vue','config','common','../model/upLoadList','../model/downLoadList',
	'vue-lazyload'],
	function(Vue,config,utils,upLoadList,downLoadList,VueLazyload){
	'use strict';

	Vue.use(VueLazyload, {
	  preLoad: 1.3,
	  error: '../../../image/loreList@2x.png',
	  loading: '../../../image/loadingUP.gif'
	})

	var allianceVue = new Vue({
		el:'#app',
		data:{
			provinceInitList:[{id:'0', name:'全国'}],
			cityInitList:[{id:'0', name:'全省'}],
			countyInitList:[{id:'0', name:'全城'}],	
			provinceList:'',
			cityList:'',
			countyList:'',		
			list: '',	
			page: 1,
			totalPage:0,
			isLoadMore:0,
			pageSize:'',
			province: 0,
			city: 0,
			county: 0
		},
		filters:{
			maxLenTitle:function(value){
				value = value.toString();
				if(value.length>16){
					return value.slice(0,16)+'...';
				}else{
					return value;
				}
			},
			maxLenAddress:function(value){
				value = value.toString();
				if(value.length>40){
					return value.slice(0,40)+'...';
				}else{
					return value;
				}
			}
		},
		watch:{
			list:function(to,from){
				if(to.length === 0 && this.list !== ''){
					$('#noData').show();
				}else{
					$('#noData').hide();
				}
			}
		},
		created:function(){	
			var _this=this;
			_this.province = utils.getData("province") || 0;
			_this.city = utils.getData("city") || 0;
			_this.county = utils.getData("county") || 0;

			var provinceText = utils.getData("provinceText") || '';
			var cityText = utils.getData("cityText") || '';
			var countyText = utils.getData("countyText") || '';
			if (countyText !== '') {
				countyText = countyText === "全城" ? cityText : countyText;
				$("#selReginText").text(countyText);
			} else if(cityText !== '') {
				cityText = cityText === "全省" ? provinceText : cityText;
				$("#selReginText").text(cityText);
			} else {
				$("#selReginText").text('全国');
			}
			if (_this.province !== 0) {
				utils.ajax(utils.getWebUrl('getRegionByParentId'),{parentId: _this.province},'POST',function(res){
					_this.cityList = _this.cityInitList.concat(res.data);
					_this.countyList = '';
				})
			}

					
			//_this.pageSize =  parseInt(Math.ceil(($('body').height()-$('.headTitle').height()-$('.title').height())/90), 10);
			_this.pageSize = 15;
			var data = {
				page: _this.page,
				pageSize: _this.pageSize,
				province: _this.province,
				city: _this.city,
				county: _this.county
			}
			upLoadList.ajax(utils.getWebUrl('shop/list'),data,function(res){
				_this.totalPage = res.data.totalPage;
				//_this.page++;
				// for(var i=0;i<res.data.list.length;i++){
				// 	if(res.data.list[i].shopsPic==null || res.data.list[i].shopsPic==''){
    //        				res.data.list[i].shopsPic = res.data.list[i].facadePhoto;
	   //         		}else{
	   //         			res.data.list[i].shopsPic = res.data.list[i].shopsPic;
	   //         		}
				// }
				_this.list = res.data.list;

				utils.ajax(utils.getWebUrl('getRegionByParentId'),{
					parentId: 1
				},'POST',function(res){
					_this.provinceList = _this.provinceInitList.concat(res.data);
				},null,null,null)
				 
			})
		},
		directives: {
		  scroll: {
		    bind: function (el, binding){
		      window.addEventListener('scroll',function() {
		       		var fnc = binding.value;   
   					fnc();
		      })
		    }
		  }
		},
		methods:{
			toRefresh: function(){
				window.location.reload();
			},
			toIndex: function(){
				var userTypeId =utils.getData('userTypeId');
				if(userTypeId == 4){
					window.location.href='userInfo.html';
				}else{
					window.location.href='shopInfo.html';
				}
			},
			selRegion:function(event){
				var _this = this;
				var $target = $(event.currentTarget);
				if($target.hasClass('down')){
					$target.removeClass('down');
					$target.addClass('up');
				}else{
					$target.addClass('down');
					$target.removeClass('up');
				}
				/*var data = {
					parentId: 1
				}*/
				if($("#region").is(":hidden")){
				    $("#region").show();    //如果元素为隐藏,则将它显现
				    /*upLoadList.ajax(utils.getWebUrl('getRegionByParentId'),data,function(res){
						_this.provinceList = _this.provinceInitList.concat(res.data);
						$("#region").show();
					})*/
				}else{
				    $("#region").hide();     //如果元素为显现,则将其隐藏
				}
					
			},
			selProvince:function(event){
				var _this = this;
				var $target = $(event.currentTarget);
				$target.addClass("active");
				$target.parents('li').siblings().find('a').removeClass('active');
				$('.county').hide();

				var id = $target[0].dataset.id;
				$("#selProvinceV").val(id);
				$("#selProvinceT").val($target.text());
				// 保存选择的省id
				utils.setData("province",id);
				utils.setData("provinceText",$target.text());
				utils.setData("cityText",'');
				utils.setData("countyText",'');
				utils.setData("city",'');
				utils.setData("county",'');
				//城市列表移除active
				$('.city').find('a').removeClass('active');
				if(id!=0){
					$('.city').show();
					$('.county').hide();
					var data = {
						parentId: id
					}
					utils.ajax(utils.getWebUrl('getRegionByParentId'),data,'POST',function(res){
						_this.cityList = _this.cityInitList.concat(res.data);
						_this.countyList = '';
					})
				}else{
					$('#selReginText').addClass('down');
					$('#selReginText').removeClass('up');

					$("#region").hide();
					$("#selReginText").text($("#selProvinceT").val());

					var _this=this;
					_this.page = 1;
					var data = {
						page: _this.page,
						pageSize: _this.pageSize,
						province:0,
						city:0,
						county:0
					}
					//_this.list = '';
					upLoadList.ajax(utils.getWebUrl('shop/list'),data,function(res){
						_this.totalPage = res.data.totalPage;
						// for(var i=0;i<res.data.list.length;i++){
						// 	if(res.data.list[i].shopsPic==null || res.data.list[i].shopsPic==''){
		    //        				res.data.list[i].shopsPic = res.data.list[i].facadePhoto;
			   //         		}else{
			   //         			res.data.list[i].shopsPic = res.data.list[i].shopsPic;
			   //         		}
						// }
						_this.list = res.data.list;
						_this.cityList = '';
						_this.countyList = '';

						_this.province = 0;
						_this.city = 0;
						_this.county = 0;
					})
				}
						
			},	
			selCity:function(event){
				var _this = this;
				var $target = $(event.currentTarget);
				$target.addClass("active");
				$target.parents('li').siblings().find('a').removeClass('active');

				$('.county').show();
				var id = $target[0].dataset.id;
				$('#selCityV').val(id);
				$("#selCityT").val($target.text());
				// 保存选择的市id
				utils.setData("city",id);
				utils.setData("cityText",$target.text());
				if(id==0){
					$("#region").hide();
					$('#selReginText').addClass('down');
					$('#selReginText').removeClass('up');

					//var province = $("#selProvinceV").val();
					var province = $('.province .active').data('id');
					//$("#selReginText").text($("#selProvinceT").val());
					$("#selReginText").text($(".province .active").text());
					_this.page = 1;
					var data = {
						page: _this.page,
						pageSize: _this.pageSize,
						province:province,
						city:0,
						county:0
					}
					//_this.list = '';
					upLoadList.ajax(utils.getWebUrl('shop/list'),data,function(res){
						_this.totalPage = res.data.totalPage;
						// for(var i=0;i<res.data.list.length;i++){
						// 	if(res.data.list[i].shopsPic==null || res.data.list[i].shopsPic==''){
		    //        				res.data.list[i].shopsPic = res.data.list[i].facadePhoto;
			   //         		}else{
			   //         			res.data.list[i].shopsPic = res.data.list[i].shopsPic;
			   //         		}
						// }
						_this.list = res.data.list;

						_this.province = province;
						_this.city = 0;
						_this.county = 0;
					})
				}else{
					$("#selCityV").val(id);
					$("#selReginText").text($target.text());
					var data = {
						parentId: id
					}
					utils.ajax(utils.getWebUrl('getRegionByParentId'),data,'POST',function(res){
						_this.countyList = _this.countyInitList.concat(res.data);
					})	
				}

			},
			selCounty:function(event){
				var _this = this;
				var $target = $(event.currentTarget);
				$(".three .active").removeClass("active"); 
				$target.addClass("active"); 
				var id = $target.data('id');
				//var province = $("#selProvinceV").val();
				var province = $('.province .active').data('id');
				var city = $("#selCityV").val();
				$("#selCountyV").val(id);
				$("#selCountyT").val($target.text());
				if($target.text().indexOf('全城')){
					$("#selReginText").text($target.text());
				}else{
					$("#selReginText").text($('.city .active').text());
				}
				
				$("#region").hide();
				_this.page = 1;
				var data = {
					page: _this.page,
					pageSize: _this.pageSize,
					province:province,
					city:city,
					county:id
				}
				// 保存选择的区id
				utils.setData("countyText",$target.text());
				utils.setData("county",id);

				//_this.list = '';
				upLoadList.ajax(utils.getWebUrl('shop/list'),data,function(res){
					_this.totalPage = res.data.totalPage;
					// for(var i=0;i<res.data.list.length;i++){
					// 	if(res.data.list[i].shopsPic==null || res.data.list[i].shopsPic==''){
	    //        				res.data.list[i].shopsPic = res.data.list[i].facadePhoto;
		   //         		}else{
		   //         			res.data.list[i].shopsPic = res.data.list[i].shopsPic;
		   //         		}
					// }
					_this.list = res.data.list;


					_this.province = province;
					_this.city = city;
					_this.county = id;

				})
			},
			toDetils:function(event){
				var $id = $(event.currentTarget).parents('dl').data('id');
				window.location.href="allianceDetail.html?shopsId="+$id;
			},
			loadMore:function(event){
				var _this = this;
				if ((upLoadList.getScrollTop() + upLoadList.getClientHeight() >= upLoadList.getScrollHeight())&&_this.page<=_this.totalPage) {				
					_this.page = _this.page+1;
					var province = $("#selProvinceV").val();
					var city = $("#selCityV").val();
					var county = $("#selCountyV").val();
		           	var data = {
						page: _this.page,
						pageSize: _this.pageSize,
						province:_this.province,
						city:_this.city,
						county:_this.county
					}
					upLoadList.ajax(utils.getWebUrl('shop/list'),data,function(res){
						_this.list = _this.list.concat(res.data.list);
						_this.totalPage = res.data.totalPage;
					})
		        }
			},

			Start:function(event){
				var touch = event.targetTouches[0];
				this.start = touch.pageY;
			},

			Move:function(event){
				var touch = event.targetTouches[0];
				this.end = (this.start - touch.pageY);
				var slideDown1 = $('#pullDown1');
				//下滑才执行操作 
                if(this.end < 0){
                	
                	slideDown1.text('下拉刷新↓');
	        		slideDown1.show();
	        		slideDown1[0].style.height =  (parseInt("0px")-this.end) + "px";
	        		if(this.end < -48){
	        			/*slideDown1.hide(); 
	        			downLoadList.slideDownStep1(this.end);*/
	        			slideDown1.text('松手开始更新↑')
	        		}
                }else{
                	slideDown1.hide();
                }
			},

			End:function(event){
				var _this = this;
				var slideDown1 = $('#pullDown1');
				if(this.end >0){  
					slideDown1.hide();
	            }else{ 

	                if(this.end < -48 && document.body.scrollTop == 0){
	                	var slideDown1 = $('#pullDown1');
	                	var slideDown2 = $('#pullDown2');
						//$('.pullDownLabel').text("下拉刷新...");
						slideDown1.hide();
				        slideDown2.show();

	                	downLoadList.slideDownStep2(); 
		                //刷新成功则 
		                //模拟刷新成功进入第三步
		                $('#pullDown3').hide();
		                //请求数据
		                _this.page = 1;
		                var data = {
							page: _this.page,
							pageSize: _this.pageSize,
							province:_this.province,
							city:_this.city,
							county:_this.county
						}

						//_this.list = '';
		                
						utils.ajax(utils.getWebUrl('shop/list'),data,
							'POST',
							function(res){
	                		_this.totalPage = res.data.totalPage;
							//_this.page++;
							_this.list = res.data.list;
						},null,function(){
							downLoadList.slideDownStep3();
						})
		                
	                }else{
	                	var slideDown1 = $('#pullDown1');
		        		slideDown1.hide();
	                }
	            }
			}
		}
		
	});


	return allianceVue
});